package web.admin.config;

import domain.MyConfiguration;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.ConfigurationService;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Display settings for project
 */
public final class ConfigurationPanel extends Panel {

    @SpringBean
    private ConfigurationService configurationService;
    private final static Logger logger = Logger.getLogger(ConfigurationPanel.class.getName());

    public ConfigurationPanel(String id) {
        super(id);
        IModel<MyConfiguration> configModel = new LoadableDetachableModel<MyConfiguration>() {

            @Override
            protected MyConfiguration load() {
                return configurationService.getConfig();
            }
        };
        add(new Form<MyConfiguration>("form", configModel) {

            {
                add(new TextField("administratorEmail", new PropertyModel(getModel(), "administratorEmail")));
                add(new TextField("imageDirectory", new PropertyModel(getModel(), "imageDirectory")));
                add(new TextField("xmlDirectory", new PropertyModel(getModel(), "xmlDirectory")));
                add(new TextField("contentInvetoryDir", new PropertyModel(getModel(), "contentInvetoryDir")));
                add(new TextField("domainName", new PropertyModel(getModel(), "domainName")));

            }

            @Override
            protected void onSubmit() {

                try {
                    configurationService.updateConfiguration(getModelObject());
                    info(getLocalizer().getString("saved", this));

                } catch (Exception e) {
                    logger.log(Level.SEVERE, null, e);
                    error(getLocalizer().getString("system.error", this));
                }

            }
        });
    }

}
